<template>
  <div id="interactTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="来源" prop="originTopicType">
          <el-select
            v-model="formInline.originTopicType"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in sourceList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 输入搜索部分 -->
        <el-form-item label="内容" prop="content">
          <el-input
            clearable
            v-model="formInline.content"
            placeholder="请输入消息内容搜索"
            size="small"
            class="numberInput"
          ></el-input>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'interactTopSearch',
  msg: '互动消息管理-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          originTopicType: '', // 来源,默认全部
          content: '', // 搜索框内容
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      // 来源列表
      sourceList: [{ dictLabel: '全部', dictValue: '' }],
      rules: {
        originTopicType: {},
        content: {},
        createTime: { type: 'array' }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getSourceList()
  },
  methods: {
    // 获取来源
    async getSourceList() {
      let sourceList = await sysNormalDisable(
        'sys_category_type',
        'messageType'
      )
      this.sourceList = this.sourceList.concat(sourceList)
    },
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
    }
  }
}
</script>

<style lang="less">
#interactTopSearch {
  .search-box {
    padding-left: 30px !important;
    margin-bottom: 10px !important;
  }
  .el-input {
    width: 70%;
  }
  .numberInput {
    width: 200px;
  }
}
</style>
